import * as XLSX from "xlsx";

// 导出表格数据
export const exportTable = (columns: any[], data: any[]) => {
  // Step 1: 获取表头
  const headers = columns.map(col => col.title);

  // Step 2: 获取表格内容
  const rows = data.map(row => columns.map(col => row[col.field]));
  // Step 3: 合并表头和内容
  const worksheetData = [headers, ...rows];

  // Step 4: 创建工作簿并添加工作表
  const worksheet = XLSX.utils.aoa_to_sheet(worksheetData);
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, "Table");

  // Step 5: 导出 Excel 文件
  XLSX.writeFile(workbook, `data_${new Date().getTime()}.xlsx`);
};
